package com.localization;

import android.util.Log;

/**
 * Created by Edward on 2017/1/7.
 */
public class Utils {
    public final static int MinSocCmdPacketLength = 13;
    public final static int SOC_MSG_FLAG = 0xFC;
    public final static int SOC_MSG_LEN_POS = 0x01;
    public final static int CMD_MSG_LEN0_POS = 0x01;
    public final static int CMD_MSG_LEN1_POS = 0x02;
    public static HostCmd mHostCmd = new HostCmd();

    public static boolean CheckFcs(byte cmd[],int len)
    {
        int cnt = 0;
        int result = 0;
        for(cnt = 1;cnt < len;cnt++)
        {
            result ^=(cmd[cnt]&0xff);
            //String hex = Integer.toHexString(cmd[cnt] & 0xFF);
           // System.out.print(hex+" ");
        }

        //System.out.println("reslut = "+result);

        if(result == 0)
            return true;

        return false;
    }

    public static int lookupSocFirstPacketHeader(byte[] cmds,int len)
    {
        for(int index = 0;index < len;index++)
        {
            if((cmds[index]&0xff) == SOC_MSG_FLAG)
            {
                return index;
            }
        }

        return -1;
    }

    /*
        数据包接收处理
     */
    public static HostCmd CmdPorcess(GatawayBuffer buffer)
    {
        int packetHeaderPos = 0;
        int cmdPacketLen = 0;
        byte[] cmds = buffer.getBuffer();
        while(buffer.getPostion() >= MinSocCmdPacketLength)
        {
            packetHeaderPos = lookupSocFirstPacketHeader(buffer.getBuffer(),buffer.getPostion());
            if(packetHeaderPos == -1)
            {
                buffer.setCleanBuf();
                return null;
            }

            if(packetHeaderPos != 0)
            {
                buffer.setMove(packetHeaderPos);
            }

            cmdPacketLen = (((cmds[CMD_MSG_LEN0_POS]&0xff)<<8)&0xff00)|(cmds[CMD_MSG_LEN1_POS]&0xff);

            if((cmdPacketLen+4) > buffer.getPostion())
            {
                System.out.println("[localServer]: Packet length error,Wait to receive other datas\n");
                Log.d("Packet:","Length error");
                break;
            }

            if(CheckFcs(cmds,cmdPacketLen+4))
            {
                Log.d("走进来了","走进来了");
               // result =  Arrays.copyOf(cmds,cmdPacketLen+4);
                //mHostCmd = new HostCmd(cmds,cmdPacketLen+4);
                mHostCmd.AddData(cmds,cmdPacketLen+4);
            }
            else
            {
                Log.d("FCS ","error");
                buffer.setMove((cmdPacketLen+4));
            }

            if(buffer.getPostion() >= (cmdPacketLen+4))
            {
                buffer.setMove((cmdPacketLen+4));
            }
            /*
            else {
                buffer.setCleanBuf();
            }
            */
            if (mHostCmd.getLength() > 0)
                return mHostCmd;
        }
        return null;
    }


    public static String  printHexString( byte[] b,int len) {
        String cmd="";
        for (int i = 0; i < len; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            cmd=cmd+hex.toUpperCase();
        }
      return cmd;
    }



    public static String IntToHexString(int data)
    {
        String hex = Integer.toHexString(data);
        //System.out.println("hex len = "+hex.length());
        if((hex.length()%2) != 0)
        {
            hex = '0' + hex;
        }
        hex = "0x"+hex;
        return hex;
    }
}
